package com.flower.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.flower.pojo.Comment;
import com.flower.pojo.Product;
import com.flower.service.ICommentService;
import com.flower.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 评论模块
 */
@Controller
public class CommentController {
	
	@Resource(name = "productService")
    private IProductService iProductService;
	@Resource(name = "commentService")
    private ICommentService iCommentService;
	
	/**
	 * 后台展示所有的评论
	 * @param startPage
	 * @param model
	 * @return
	 */
	@RequestMapping("/selectAllComments.do")
	public String selectAllComments(@RequestParam(required=true, defaultValue="1") Integer startPage,
			Model model){
		PageHelper.startPage(startPage, 10);
		List<Comment> comments = iCommentService.selectAllComments();
		PageInfo<Comment> pi = new PageInfo<>(comments);
		model.addAttribute("PAGE", pi);
		model.addAttribute("COMMENTS", comments);
		
		return "message_manage";
	}
	
	/**
	 * 插入评价
	 */
	@RequestMapping("/addComment.do")
	@ResponseBody
	public String insertComment(HttpServletRequest request, Comment comment){
		comment.setCmtime(new Date());
		comment.setCmimage("product/" + comment.getCmimage());
		int row = iCommentService.insertComment(comment);
		
		if(row > 0){
			return "OK";
		}else{
			return "FAIL";
		}
	}
	
	/**
	 * 进入评价界面
	 */
	@RequestMapping("/showProductToComment.do")
	public String showProductToComment(HttpServletRequest request, int pid){
		Product product = iProductService.findProductByPid(pid);
		request.getSession().setAttribute("Product", product);
		return "redirect:/comment.jsp";
	}
	
}
